package com.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.ysh.gsm.vos.DoubleSerice;


public class DoubleMRCalc {
	
	public static List<DoubleSerice> calTotal(int min,int max,
		List<DbMRData> source1,List<DbMRData> source2, long total) {
		
		double y1=0;
		double y2=0;
		long sum1=0;
		long sum2=0;
		int posionY1=0;
		int posionY2=0;
		boolean yflag=true;
		boolean yyflag=true;
	
		if (total == 0 || source1 == null || source1.size() == 0) {
			yflag=false;
		}
		if (total == 0 || source2 == null || source2.size() == 0) {
			yyflag=false;
		}
		int len1 =0;
		int len2 = 0;
		if(yflag){
			len1=source1.size();
			Collections.sort(source1, new CustomComparator());	
		}
		if(yyflag){
			len2=source2.size();
			Collections.sort(source2, new CustomComparator());	
		}
		List<DoubleSerice> result = new ArrayList<DoubleSerice>();
		for(int i=min;i<max;i++){
			if(yflag){
							
				for (int y = posionY1; y < len1; y++) {
					
					if(source1.get(y).getVal()<=i){	
						sum1+=source1.get(y).getCount();
						if(y+1==len1){
							y1=(double)sum1*100/total;
							posionY1=y;	
							break;
						}
					}else{
						y1=(double)sum1*100/total;
						posionY1=y;	
						break;
					}
					
				}
			}
			if(yyflag){
							
				for (int z = posionY2; z < len2; z++) {
					
					if(source2.get(z).getVal()<=i){		
						sum2+=source2.get(z).getCount();
						if(z+1==len2){
							y2=(double)sum2*100/total;
							posionY2=z;
							break;
						}
					}else{
						y2=(double)sum2*100/total;
						posionY2=z;
						break;
						
					}
					
				}
			}
			result.add(new DoubleSerice(i,y1,y2));
		}
		
		
		return result;
	}

	private static class CustomComparator implements Comparator<DbMRData> {
		public int compare(DbMRData o1, DbMRData o2) {
			return (o1.getVal() > o2.getVal() ? 1 : (o1.getVal() == o2
					.getVal() ? 0 : -1));

		}
	}

}
